/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package fr.jade.examples.fraclite.orb.consoleha;
import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.factory.GenericFactory;
import org.objectweb.fractal.api.type.ComponentType;
import org.objectweb.fractal.api.type.InterfaceType;
import org.objectweb.fractal.api.type.TypeFactory;
import org.objectweb.fractal.util.Fractal;

import fr.jade.examples.fraclite.orb.evalperf.Service;
import fr.jade.fraclite.orb.registry.NamingService;
import fr.jade.fraclite.orb.registry.Registry;

/**
 * 
 * @author Sylvain Sicard
 *
 */
public class HelloWorldLauncher {

	public static void main (final String[] args) throws Exception {
		NamingService ns = Registry.getRegistry();
		Component remoteBoot = ns.lookup("bootstrap");
		Component localBoot = Fractal.getBootstrapComponent();
		long begin = System.currentTimeMillis();
		GenericFactory remotecf = Fractal.getGenericFactory(remoteBoot);
		GenericFactory localcf = Fractal.getGenericFactory(localBoot);
		TypeFactory tf = Fractal.getTypeFactory(localBoot);
		
		// type of server component
		ComponentType sType = tf.createFcType(new InterfaceType[] {
				tf.createFcItfType("s", "fr.jade.examples.fraclite.orb.consoleha.Service", false, false, false)});

		// type of client component
		ComponentType cType = tf.createFcType(new InterfaceType[] {
				tf.createFcItfType("m", "java.lang.Runnable", false, false, false),
				tf.createFcItfType("s", "fr.jade.examples.fraclite.orb.consoleha.Service", true, true, false)
		});

		// create client component
		Component cComp = localcf.newFcInstance(cType, "primitive", "fr.jade.examples.fraclite.orb.consoleha.ClientImpl");

	  // create server component
    Component sComp = remotecf.newFcInstance(sType, "primitive", "fr.jade.examples.fraclite.orb.consoleha.ServerImpl");

		Fractal.getNameController(cComp).setFcName("Client");
		Fractal.getNameController(sComp).setFcName("Server");
		
		Service s = (Service)sComp.getFcInterface("s");
		Fractal.getBindingController(cComp).bindFc("s", s);

		// Start Root component
		Fractal.getLifeCycleController(cComp).startFc();
		long end = System.currentTimeMillis();
		System.out.println("Time to boot :"+(end-begin)/1000);
		//call main method
		((Runnable)cComp.getFcInterface("m")).run();
	}
}
